package chapter_22.io.stream;

import java.io.*;

/**
 * 利用输入、输出字节流复制文件
 */
public class FileCopy {

    public static void main(String[] args) {

        new File("./src/chapter_22/io/stream/copy_build.txt").delete();// 每次运行前删除复制文件

        // 开始时间
        long startTime = System.nanoTime();

        try (FileInputStream inputStream = new FileInputStream("./src/chapter_22/io/stream/new_build.txt");
             FileOutputStream outputStream = new FileOutputStream("./src/chapter_22/io/stream/copy_build.txt")
        ) {

            // 准备一个缓冲区
            byte[] bytes = new byte[1024];
            // 首先读取一次
            int len = inputStream.read(bytes);

            while (len != -1) {
                String copyStr = new String(bytes);
                // 打印复制的字符串
                System.out.println(copyStr);
                // 开始写入数据
                outputStream.write(bytes, 0, len);
                len = inputStream.read(bytes);
            }

            System.out.println("useTime = " + (System.nanoTime() - startTime) + "纳秒" );

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
